<?php
namespace App\Api\Controllers\Pay;

use App\Api\Bases\CommonController;
use App\Api\Models\CmcOrderList;
use App\Api\Services\JsonService;
use App\Api\Services\Pay\Alipay;
use Illuminate\Http\Request;


class  IsNotPay extends CommonController
{
    /*
     * 查询未付订单
     */
     public function orderList(Request $request)
     {

         if (!$_SERVER['REQUEST_METHOD'] === 'GET') return JsonService::fail('请求方式错误', '', config('code.HTTP_FAIL'));

         if (empty($request->get('uid'))  || empty($request->get('type')) || empty($request->get('pay_type'))) {
             return JsonService::fail('请求类型错误', '', config('code.HTTP_FAIL'));
         }
         $OrderList = CmcOrderList::orderWhere($request->get('type'), $request->get('uid'), $request->get('cart_id'));
         return JsonService::success('查询成功',$OrderList,config('code.HTTP_OK'));
     }

    /**
     * 判断使用那种方式支付
     * @param Request $request
     * @return array
     */
      public function typePay(Request $request)
      {
          if (!$_SERVER['REQUEST_METHOD'] === 'POST') return JsonService::fail('请求方式错误', '', config('code.HTTP_FAIL'));
          //订单号
          $orderSn=$request->post('order_sn');
          $totalMoney=$request->post('total_money');
          $oderlist_name=$request->post('oderlist_name');
          //支付方式
          $payType=intval($request->post('pay_type'));
          if (empty($oderlist_name) ||  empty($orderSn) || empty($totalMoney) ||  empty($request->post('pay_type'))) {
              return JsonService::fail('请求类型错误', '', config('code.HTTP_FAIL'));
          }
           //判断库存是否足够
          //[拼团0砍价1秒杀2普通3]',
          $cmcOrderListpay=CmcOrderList::CmcOrderListpay($orderSn);
         if ($cmcOrderListpay['code']==500) return JsonService::success('库存不足', '', config('code.HTTP_FAIL'));
         //支付
         // '支付类型 0账户支付 1微信支付 2支付宝支付 3银联支付',
          $payTypeDate= CmcOrderList::switchOrder($payType,$orderSn,$totalMoney,$oderlist_name);
        return $payTypeDate;
      }

      //支付宝退款
     public function  checkrefund(Request $request)
     {

         $out_trade_no=$request['order_sn'];
         if (!$_SERVER['REQUEST_METHOD'] === 'POST') return JsonService::fail('请求方式错误', '', config('code.HTTP_FAIL'));

         if ( empty($out_trade_no)) {
             return JsonService::fail('请求类型错误', '', config('code.HTTP_FAIL'));
         }
          return CmcOrderList::acheckrefund($out_trade_no);
     }
     //支付宝查账
    public static function   ordinaryPayment(Request $request)
    {
        //订单号
        $orderSn=$request->get('order_sn');
        if ( empty($orderSn)) {
            return JsonService::fail('请求类型错误', '', config('code.HTTP_FAIL'));
        }
        return  Alipay::ordinaryPayment($orderSn);
    }
    //支付宝同步回调
    public static function  aliPayReturnUrl()
    {
        return Alipay::return();
    }

}
